package org.west.sky.frame.activiti.study.chapter17;

import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.util.CollectionUtils;
import org.west.sky.frame.activiti.study.util.ActivitiEngineUtil;

import java.util.*;

/**
 * author: chz
 * date: 2025/6/6
 * description: 调用活动
 */
public class RunDemo17_2_2 extends ActivitiEngineUtil {

    public static void main(String[] args) {
        RunDemo17_2_2 runDemo16_1_2 = new RunDemo17_2_2();
        runDemo16_1_2.run();
    }

    public void run() {
        loadActivitiConfigAndInitEngine("activiti.job.xml");

        ProcessDefinition processDefinition = deployByClassPathResource("processes/chapter17/Chapter17_2_2.bpmn20.xml");
        ProcessDefinition sub_processDefinition = deployByClassPathResource("processes/chapter17/Chapter17_2_2_sub.bpmn20.xml");

        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());

        Task task1 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task1.getName() + "：任务节点");
        Map<String, Object> variables = new HashMap<>();
        variables.put("lender", "张三");
        variables.put("subProcessBusinessKey", UUID.randomUUID().toString());
        taskService.complete(task1.getId(), variables);

        Task task2 = taskService.createTaskQuery().singleResult();

        System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task2.getName() + "：任务节点");
        Map<String, Object> variables2 = new HashMap<>();
        variables2.put("amount", 5000);
        taskService.complete(task2.getId(), variables2);

        Task task3 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task3.getName() + "：任务节点");
        taskService.complete(task3.getId());


        closeEngine();
    }
}
